//
//  EFOrderDetailViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/11.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFOrderDetailViewController.h"
#import "EFOrderTotalInfoItem.h"
#import "EFOrderStatusItem.h"
#import "EFOrderNumItem.h"
#import "EFOrderTranseItem.h"
#import "EFOrderUserInfoItem.h"
#import "EFOrderProductItem.h"
#import "EFOrderOpenItem.h"
#import "EFOrderPayTypeItem.h"
#import "EFOrderPayDetailItem.h"
#import "EFOrderTotalAndTimeItem.h"
#import "EFSeporatorItem.h"
#import "EFWhiteSepetorCell.h"
#import "EFWhiteSeperatorItem.h"
#import "EFOrderSumTimeCell.h"
#import "EFOrderTotalAndTimeItem.h"
#import "EFOrderQCodeItem.h"
#import "EFOrderPayQcodeCell.h"

#import "EFOrderStatusCell.h"
#import "EFOrderNumberCell.h"
#import "EFOrderPayQcodeCell.h"
#import "EFOrderUserInfoCell.h"
#import "EFOrderProductCell.h"
#import "EFOrderShopNameCell.h"
#import "EFOrderMoreCell.h"
#import "EFOrderPayMoneyDetailCell.h"
#import "EFPayDetailTypeCell.h"
#import "EFOrderSumTimeCell.h"
#import "EFOrderTranseCell.h"
#import "EFSeperatorCell.h"
#import "iToast.h"
#import "WXApi.h"
#import "EFShoppingCartViewController.h"
#import "EFRequestReturnViewController.h"

@interface EFOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSString *orderNumber;
@property (strong, nonatomic) EFOrderTotalInfoItem *orderInfo;
@property (strong, nonatomic) UITableView *contentTableView;
@property (strong, nonatomic) NSMutableArray *dataArray;

@property (strong, nonatomic) UIView *bottomView;
@property (strong, nonatomic) UIButton *cancelOrderBtn;//绿色
@property (strong, nonatomic) UIButton *deleteOrderBtn;//灰色
@property (strong, nonatomic) UIButton *payBtn;//绿色
@property (strong, nonatomic) UIButton *returnBtn;//灰色
@property (strong, nonatomic) UIButton *buyAgainBtn;//绿色
@property (strong, nonatomic) UIButton *returnDetailBtn;//绿色
@property (strong, nonatomic) UIButton *returnRecordBtn;//绿色
@end

@implementation EFOrderDetailViewController

- (instancetype)init{
    self = [super init];
    if (self) {
         self.title = @"订单详情";
    }
    return self;
}

-(instancetype)initWithOrderNumber:(NSString *)orderNumber{
    self = [self init];
    if(self){
        self.orderNumber = orderNumber;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onWeixinPayDoneNotification:) name:kNotificationWeixinPaySuccess object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    [self.view addSubview:self.contentTableView];
    [self.view addSubview:self.bottomView];
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    bottomLine.backgroundColor = RGBCOLOR(0xececec);
    [self.bottomView addSubview:bottomLine];

    //已支付状态
    [self.bottomView addSubview:self.buyAgainBtn];
    [self.bottomView addSubview:self.returnBtn];
    self.buyAgainBtn.right = kScreenWidth - 20;
    self.buyAgainBtn.top = 13;
    self.returnBtn.right = self.buyAgainBtn.left - 10;
    self.returnBtn.top = self.buyAgainBtn.top;
    
    //未发货 待收货
    [self.bottomView addSubview:self.cancelOrderBtn];
    self.cancelOrderBtn.right = kScreenWidth - 20;
    self.cancelOrderBtn.top = 13;

    //已退货
    [self.bottomView addSubview:self.returnRecordBtn];
    [self.bottomView addSubview:self.deleteOrderBtn];
    self.returnRecordBtn.right = kScreenWidth - 20;
    self.returnRecordBtn.top = 13;
    self.deleteOrderBtn.right = self.returnRecordBtn.left - 10;
    self.deleteOrderBtn.top = self.returnRecordBtn.top;
    
    //未支付
    [self.bottomView addSubview:self.payBtn];
    self.payBtn.right = kScreenWidth - 20;
    self.payBtn.top = 13;
    
    
    [self setAllBtnHidden];
    [self requestOrderInfo];
}

- (void)setAllBtnHidden{
    self.buyAgainBtn.hidden = YES;
    self.returnBtn.hidden = YES;
    self.cancelOrderBtn.hidden = YES;
    self.returnRecordBtn.hidden = YES;
    self.deleteOrderBtn.hidden = YES;
    self.payBtn.hidden = YES;
}

//0未付款  1待收货  2  已完成  3   退换货   4线下购物
- (void)setBtns{
    self.deleteOrderBtn.hidden =  !self.orderInfo.is_show_btn_deleteorder;
    self.payBtn.hidden = !self.orderInfo.is_show_btn_paynow;
    self.returnDetailBtn.hidden = !self.orderInfo.is_show_btn_returndetail;
    self.cancelOrderBtn.hidden = !self.orderInfo.is_show_btn_cancelorder;
    self.returnBtn.hidden = !self.orderInfo.is_show_btn_returnrequest;
    self.buyAgainBtn.hidden = !self.orderInfo.is_show_btn_buyagain;
}

- (void)requestOrderInfo{
    __weak typeof(self) weakSelf = self;
    [self showHUD];
    [APP_CONTEXT.net requestOrderInfo:self.orderNumber OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        weakSelf.HUD.hidden = YES;
        if (code == 0) {
            weakSelf.orderInfo = [EFOrderTotalInfoItem yy_modelWithDictionary:info];
            [weakSelf handleData:weakSelf.orderInfo];
        }
    }];
}
- (void)back:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)fakeData{
    
    self.orderInfo = [[EFOrderTotalInfoItem alloc] init];
    self.orderInfo.order_no = @"4524524524";
    self.orderInfo.status_name = @"已付款"; //如果是已支付状态 增加支付码
    self.orderInfo.created_at = @"2017年3月2日";
    self.orderInfo.payment_time = @"2017年3月3日";
    self.orderInfo.total_amount = @"200";
    self.orderInfo.amount = @"190";
    self.orderInfo.payment_no = @"4254524525245555";
    self.orderInfo.carrier = @"顺丰";
    self.orderInfo.tracking_no = @"2389328932722,212109038229";
    self.orderInfo.post_message = @"本订单已超出退换货服务时间（10天）";
    self.orderInfo.postage = @"5";
    self.orderInfo.name = @"韩梅梅";
    self.orderInfo.phone = @"123545566";
    self.orderInfo.addr = @"北京朝阳区望京望京望京望京望京望京望京我家网我就忘记望京望京望京";
    self.orderInfo.addr_detail = @"叶青大厦D座";
    self.orderInfo.paymethod = @"微信支付";
    self.orderInfo.credit_amount = @"4";
    self.orderInfo.discount = @"10";
    self.orderInfo.coupon_amount = @"20";
    self.orderInfo.is_show_btn_paynow = YES;
    
    EFOrderProductItem *pI1 = [[EFOrderProductItem alloc] init];
    pI1.product_name = @"依云矿泉水";
    pI1.price = @"5.80";
    pI1.qty = @"5";
    pI1.model_name = @"3瓶装";
    
    self.orderInfo.detail = [[NSMutableArray alloc] init];
    [self.orderInfo.detail addObject:pI1];
    
    [self handleData:nil];
    
}


- (void)handleData:(EFOrderTotalInfoItem *)orderTotalItem{
    EFOrderStatusItem *statusItem = [[EFOrderStatusItem alloc] init];
    statusItem.orderStatus = self.orderInfo.status_name;
    [self.dataArray addObject:statusItem];
    
    EFOrderNumItem *numItem = [[EFOrderNumItem alloc] init];
    numItem.orderNum = self.orderInfo.order_no;
    [self.dataArray addObject:numItem];
    
    EFOrderQCodeItem *qcodeItem = [[EFOrderQCodeItem alloc] init];
    qcodeItem.payment_qrcode = self.orderInfo.payment_qrcode;
    qcodeItem.payment_no = self.orderInfo.payment_no;
    if (qcodeItem.payment_qrcode.length > 0) {
        [self.dataArray addObject:qcodeItem];
    }
   
    EFOrderTranseItem *transeItem = [[EFOrderTranseItem alloc] init];
    transeItem.tracking_no = self.orderInfo.tracking_no;
    transeItem.carrier = self.orderInfo.carrier;
    if (self.orderInfo.post_message.length > 0) {
        transeItem.post_message = self.orderInfo.post_message;
        [self.dataArray addObject:transeItem];
    }
    
    
    EFOrderUserInfoItem *userInfItem = [[EFOrderUserInfoItem alloc] init];
    userInfItem.name = self.orderInfo.name;
    userInfItem.phone = self.orderInfo.phone;
    userInfItem.addr = self.orderInfo.addr;
    userInfItem.addr_detail = self.orderInfo.addr_detail;
    [self.dataArray addObject:userInfItem];
    
    EFSeporatorItem *sI = [[EFSeporatorItem alloc] init];
    [self.dataArray addObject:sI];
    
    for (NSInteger index = 0; index < self.orderInfo.detail.count; index ++){
        EFOrderProductItem *item = [self.orderInfo.detail objectAtIndex:index];
        [self.dataArray addObject:item];
    }
    //添加展开cell
    if (self.orderInfo.detail.count > 2) {
        EFOrderOpenItem *openItem = [[EFOrderOpenItem alloc] init];
        openItem.count = self.orderInfo.detail.count;
        [self.dataArray addObject:openItem];
    }
    EFSeporatorItem *sI1 = [[EFSeporatorItem alloc] init];
    [self.dataArray addObject:sI1];
    
    EFOrderPayTypeItem *payTypeItem = [[EFOrderPayTypeItem alloc] init];
    payTypeItem.payType = self.orderInfo.paymethod;
    [self.dataArray addObject:payTypeItem];
    EFWhiteSeperatorItem *whiteItem = [[EFWhiteSeperatorItem alloc] init];
    [self.dataArray addObject:whiteItem];
    
    EFOrderPayDetailItem *totalMoney = [[EFOrderPayDetailItem alloc] init];
    totalMoney.rightStr = self.orderInfo.total_amount;
    totalMoney.leftStr = @"商品总额";
    [self.dataArray addObject:totalMoney];
    EFOrderPayDetailItem * transeFee = [[EFOrderPayDetailItem alloc] init];
    transeFee.rightStr = self.orderInfo.postage;
    transeFee.leftStr = @"运费";
    [self.dataArray addObject:transeFee];
    
    if (self.orderInfo.coupon_amount != nil && self.orderInfo.coupon_amount.length > 0) {
        EFOrderPayDetailItem *couponItem = [[EFOrderPayDetailItem alloc] init];
        couponItem.rightStr = [NSString stringWithFormat:@"-%@",self.orderInfo.coupon_amount];
        couponItem.leftStr = @"优惠券";
        [self.dataArray addObject:couponItem];
    }
    
    if (self.orderInfo.credit_amount != nil && self.orderInfo.credit_amount.length > 0) {
        EFOrderPayDetailItem *creditItem = [[EFOrderPayDetailItem alloc] init];
        creditItem.rightStr = [NSString stringWithFormat:@"-%@",self.orderInfo.credit_amount];
        creditItem.leftStr = @"积分抵扣";
        [self.dataArray addObject:creditItem];
    }
    
    if (self.orderInfo.discount != nil && self.orderInfo.discount > 0) {
        EFOrderPayDetailItem *discountItem = [[EFOrderPayDetailItem alloc] init];
        discountItem.rightStr = [NSString stringWithFormat:@"-%@",self.orderInfo.discount];
        discountItem.leftStr = @"折扣";
        [self.dataArray addObject:discountItem];
    }
    EFWhiteSeperatorItem *whiteItem1 = [[EFWhiteSeperatorItem alloc] init];
    [self.dataArray addObject:whiteItem1];
    
    EFOrderTotalAndTimeItem *totalTimeItem = [[EFOrderTotalAndTimeItem alloc] init];
    totalTimeItem.orderTotal= self.orderInfo.amount;
    totalTimeItem.orderTime = self.orderInfo.created_at;
    [self.dataArray addObject:totalTimeItem];
    
    EFSeporatorItem *sI2 = [[EFSeporatorItem alloc] init];
    [self.dataArray addObject:sI2];
    
    [self.contentTableView reloadData];
    
    [self setBtns];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)contentTableView{
    if (!_contentTableView) {
        _contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - 50) style:UITableViewStylePlain];
        _contentTableView.delegate = self;
        _contentTableView.dataSource = self;
        _contentTableView.backgroundColor = [UIColor whiteColor];
        _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _contentTableView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSObject *obj = [self.dataArray objectAtIndex:indexPath.row];
    if ([obj isKindOfClass:[EFOrderStatusItem class]]) {
        return 50;
    }
    if ([obj isKindOfClass:[EFOrderNumItem class]]) {
        return 45;
    }
    if ([obj isKindOfClass:[EFOrderTranseItem class]]) {
        CGFloat height = [EFOrderTranseCell cellHeight:(EFOrderTranseItem *)obj];
        return height;
    }
    if ([obj isKindOfClass:[EFOrderUserInfoItem class]]) {
        CGFloat height = [EFOrderUserInfoCell cellHeight:(EFOrderUserInfoItem *)obj];
        return height;
    }
    if ([obj isKindOfClass:[EFOrderProductItem class]]) {
        return 107;
    }
    if ([obj isKindOfClass:[EFOrderOpenItem class]]) {
        return 45;
    }
    if ([obj isKindOfClass:[EFOrderPayDetailItem class]]) {
        return 22;
    }
    if ([obj isKindOfClass:[EFOrderTotalAndTimeItem class]]) {
        return 72;
    }
    if ([obj isKindOfClass:[EFOrderPayTypeItem class]]) {
        return 45;
    }
    if ([obj isKindOfClass:[EFSeporatorItem class]]) {
        return 10;
    }
    if ([obj isKindOfClass:[EFWhiteSeperatorItem class]]) {
        return 10;
    }
    if ([obj isKindOfClass:[EFOrderQCodeItem class]]) {
        return 168;
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSObject *obj = [self.dataArray objectAtIndex:indexPath.row];
    UITableViewCell *cell = nil;
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"empty"];
    if ([obj isKindOfClass:[EFOrderStatusItem class]]) {
        cell = [[EFOrderStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"status"];
        [(EFOrderStatusCell *)cell setObject:(EFOrderStatusItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderNumItem class]]) {
        cell = [[EFOrderNumberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"number"];
        [(EFOrderNumberCell *)cell setObject:(EFOrderNumItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderTranseItem class]]) {
        cell = [[EFOrderTranseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"transe"];
        [(EFOrderTranseCell *)cell setObject:(EFOrderTranseItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderUserInfoItem class]]) {
        cell = [[EFOrderUserInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userInfo"];
        [(EFOrderUserInfoCell *)cell setObject:(EFOrderUserInfoItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderProductItem class]]) {
        cell = [[EFOrderProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"producet"];
        [(EFOrderProductCell *)cell setObject:(EFOrderProductItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderOpenItem class]]) {
        cell = [[EFOrderMoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"more"];
        [(EFOrderMoreCell *)cell setObject:(EFOrderOpenItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderPayDetailItem class]]) {
        cell = [[EFPayDetailTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"payDetail"];
        [(EFPayDetailTypeCell *)cell setObject:(EFOrderPayDetailItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderTotalAndTimeItem class]]) {
        cell = [[EFOrderSumTimeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sumTime"];
        [(EFOrderSumTimeCell *)cell setObject:(EFOrderTotalAndTimeItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderPayTypeItem class]]) {
        cell = [[EFOrderPayMoneyDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"moneyType"];
        [(EFOrderPayMoneyDetailCell *)cell setObject:(EFOrderPayTypeItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFSeporatorItem class]]) {
        cell = [[EFSeperatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"seperator"];
        return cell;
    }
    if ([obj isKindOfClass:[EFWhiteSeperatorItem class]]) {
        cell = [[EFWhiteSepetorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"whiteSeperator"];
        return cell;
    }
    if ([obj isKindOfClass:[EFOrderQCodeItem class]]) {
        cell = [[EFOrderPayQcodeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qcode"];
        [(EFOrderPayQcodeCell *)cell setObject:(EFOrderQCodeItem *)obj];
        return cell;
    }
    return cell;

}

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 50 - kStatusBarHeight, kScreenWidth, 50)];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

-(UIButton *)cancelOrderBtn{
    if(_cancelOrderBtn == NULL){
        _cancelOrderBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_cancelOrderBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_cancelOrderBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        _cancelOrderBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_cancelOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        [_cancelOrderBtn addTarget:self action:@selector(cancelOrder) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelOrderBtn;
}

-(UIButton *)deleteOrderBtn{
    if(_deleteOrderBtn == NULL){
        _deleteOrderBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_deleteOrderBtn setTitleColor:kBlack999 forState:UIControlStateNormal];
        [_deleteOrderBtn setBackgroundImage:IMG(@"linebutton_bg_gray") forState:UIControlStateNormal];
        _deleteOrderBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_deleteOrderBtn setTitle:@"删除订单" forState:UIControlStateNormal];
        [_deleteOrderBtn addTarget:self action:@selector(deleteOrder) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteOrderBtn;
}

-(UIButton *)payBtn{
    if(_payBtn == NULL){
        _payBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_payBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_payBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        _payBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_payBtn setTitle:@"付款" forState:UIControlStateNormal];
        [_payBtn addTarget:self action:@selector(payOrder) forControlEvents:UIControlEventTouchUpInside];
    }
    return _payBtn;
}

-(UIButton *)returnBtn{
    if(_returnBtn == NULL){
        _returnBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 88, 25)];
        [_returnBtn setTitleColor:kBlack999 forState:UIControlStateNormal];
        [_returnBtn setBackgroundImage:IMG(@"linebutton_bg_gray") forState:UIControlStateNormal];
        _returnBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_returnBtn setTitle:@"申请退换货" forState:UIControlStateNormal];
        [_returnBtn addTarget:self action:@selector(returnOrder) forControlEvents:UIControlEventTouchUpInside];
    }
    return _returnBtn;
}

-(UIButton *)buyAgainBtn{
    if(_buyAgainBtn == NULL){
        _buyAgainBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_buyAgainBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_buyAgainBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        _buyAgainBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_buyAgainBtn setTitle:@"再次购买" forState:UIControlStateNormal];
        [_buyAgainBtn addTarget:self action:@selector(buyAgainOrder) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buyAgainBtn;
}

- (UIButton *)returnDetailBtn{
    if(_returnDetailBtn == NULL){
        _returnDetailBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_returnDetailBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_returnDetailBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        _returnDetailBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_returnDetailBtn setTitle:@"退货详情" forState:UIControlStateNormal];
        [_returnDetailBtn addTarget:self action:@selector(returnDetail) forControlEvents:UIControlEventTouchUpInside];
    }
    return _returnDetailBtn;

}

- (UIButton *)returnRecordBtn{
    if (!_returnRecordBtn) {
        _returnRecordBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_returnRecordBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_returnRecordBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        _returnRecordBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_returnRecordBtn setTitle:@"退货记录" forState:UIControlStateNormal];
        [_returnRecordBtn addTarget:self action:@selector(returnRecord) forControlEvents:UIControlEventTouchUpInside];
    }
    return _returnRecordBtn;
}

- (void)payOrder{
    //支付订单
    [self.HUD show:YES];
    [APP_CONTEXT.net payNowWithOrderNO:self.orderNumber OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [self.HUD hide:YES];
        if(code == 0){
            [self handleWeixinPayActionResponse:info];
        }else{
            [[iToast makeText:message]show];
        }
    }];
}


#pragma mark --WeiXinPay

- (void)handleWeixinPayActionResponse:(NSDictionary *)result {
    //    if (![WXApi isWXAppInstalled]) {
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请安装微信并登录您的帐号" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    //        [alert show];
    //        return;
    //    }
    //    if (![WXApi isWXAppSupportApi]) {
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"您的微信版本不支持开放平台,请更新您的微信版本!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    //        [alert show];
    //        return;
    //    }
    
    [self.HUD hide:YES];
    NSString *noncestr = result[@"noncestr"];
    NSString *package = @"Sign=WXPay";// result[@"package"];//@"Sign=WXPay";
    NSString *prepayId = result[@"prepayid"];
    NSString *partnerId = result[@"partnerid"];
    NSString *sign = result[@"sign"];
    NSString *timeStamp = result[@"timestamp"];
    if (result[@"orderNo"] != nil) {
        self.orderNumber = result[@"orderNo"];
    }
    PayReq *request = [[PayReq alloc] init];
    request.partnerId = partnerId;
    request.prepayId = prepayId;
    request.package = package;
    request.nonceStr= noncestr;
    request.timeStamp = [timeStamp intValue];
    request.sign= sign;
    
    [WXApi sendReq:request];
}

- (void)onWeixinPayDoneNotification:(NSNotification *)notification{
    [self onWeixinPaymentResult:notification.object];
}

- (void)onWeixinPaymentResult:(PayResp *)resp {
    NSInteger statusCode = resp.errCode;
    if (statusCode == 0) {
        [self onPayDone:EWXinPay];
    } else if (statusCode != -2){
        [[iToast makeText:resp.errStr] show];
    } else {
        [[iToast makeText:@"支付不成功"] show];
    }
}

- (void)onPayDone:(NSInteger)payType{
    [[iToast makeText:@"您的订单支付成功"] show];
    //跳转订单详情页
    EFOrderDetailViewController *vc = [[EFOrderDetailViewController alloc] initWithOrderNumber:self.orderNumber];
    [ICNavigator pushViewController:vc animated:YES];
}

- (void)cancelOrder{
    [APP_CONTEXT.net cancelOrder:self.orderInfo.id OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if (code == 0) {
            [[iToast makeText:@"取消成功"] show];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [[iToast makeText:message] show];
        }

    }];
}

- (void)deleteOrder{
    [APP_CONTEXT.net removeOrder:self.orderInfo.id OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if (code == 0) {
            [[iToast makeText:@"删除成功"] show];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [[iToast makeText:message] show];
        }
    }];
}

- (void)returnOrder{
    //跳转至退换货流程
    EFRequestReturnViewController *returnVC = [[EFRequestReturnViewController alloc] initWithOrderNo:self.orderNumber];
    returnVC.orderId = self.orderInfo.id;
    [ICNavigator pushViewController:returnVC animated:YES];
    
}

- (void)buyAgainOrder{
    //再来一单的逻辑  根据order_no拉取返回的数据 跳转到购物车页面
    __weak typeof(self) weakSelf = self;
    [self.HUD show:YES];
    [APP_CONTEXT.net orderAgainWithOrderNO:self.orderNumber OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        weakSelf.HUD.hidden = YES;
        if (code == 0) {
            //跳转到商城的购物车里
            EFShoppingCartViewController *shopCart = [[EFShoppingCartViewController alloc] initWithType:0];
            [ICNavigator pushViewController:shopCart animated:YES];
        }else{
            [[iToast makeText:message] show];
        }
    }];
}

- (void)returnDetail{
}

- (void)returnRecord{
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
